import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class GoogleParser 
{
	private StringBuffer buffer;
		
	public GoogleParser(StringBuffer buffer)
	{
		this.buffer = buffer;
	}
	
	public int getTotalCitations()
	{
		int total = 0;
		String str = "";
		Pattern cites = Pattern.compile(">Cited by [0-9]+</a>", Pattern.CASE_INSENSITIVE);
		Matcher matcher = cites.matcher(buffer);
		while(matcher.find())
		{
			str += matcher.group();
		}
		
		Pattern tidy = Pattern.compile("[0-9]+");
		matcher = tidy.matcher(str);
		while(matcher.find())
		{
			total += Integer.parseInt(matcher.group());
		}
		return total;
	}
	
	public int getPageHighest()
	{
		int highResult = 0;
		String str = "";
		Pattern totalPapers = Pattern.compile("</b> - <b>[0-9]+</b>", Pattern.CASE_INSENSITIVE);
		Matcher matcher = totalPapers.matcher(buffer);
		if(matcher.find())
		{
			str = matcher.group();
		}
		System.out.println(str);
		Pattern tidy = Pattern.compile("[0-9]+");
		matcher = tidy.matcher(str);
		if(matcher.find())
		{
			highResult = Integer.parseInt(matcher.group());
		}
		
		return highResult;
	}
	
	public int getTotalPapers()
	{
		int total = 0;
		String str = "";
		Pattern totalPapers = Pattern.compile("of (about(\\s)?)?<b>[0-9]+</b>", Pattern.CASE_INSENSITIVE);
		Matcher matcher = totalPapers.matcher(buffer);
		if(matcher.find())
		{
			str = matcher.group();
		}
		System.out.println(str);
		Pattern tidy = Pattern.compile("[0-9]+");
		matcher = tidy.matcher(str);
		if(matcher.find())
		{
			total = Integer.parseInt(matcher.group());
		}
		return total;
	}
}
